<div class="container-fluid">
    <div class="wrapper">
        <div class="panel">
            <div class="panel-body">
                <form action="__URL__/grab_shipment_plan" method="get">
                    <div class="form-inline">
                        <b>帐号：</b>
                        <select class="chosen-select form-control" tabindex="-1" name="accountid"
                                data-name-group="common">
                            <option value="" class="empty-opt">-- 请选择 --</option>
                            <volist name="accounts" id="acc">
                                <option value="{$acc.id}">{$acc.name}</option>
                            </volist>
                        </select>
                        <b>shipmentid：</b><input placeholder="输入shipmentid" name="shipmentid"
                                                 data-name-group="common" type="text" value="" class="form-control">
                        <button class="btn btn-primary" type="submit">获取</button>
                    </div>
                </form>
                <hr>
                    <form class="form-horizontal">
                    <div class="col-md-4 boxalline">
                        <div class="form-group">
                            <label class="col-md-6 control-label">Shipmentid：</label>
                            <div class="col-md-6">
                                <span class="help-block m-b-none">{$plan_and_detail_data.Inbound_shipment_plan.shipmentid}</span>
                            </div>
                        </div>
                        <div class="form-group">
                            <label class="col-md-6 control-label">起运地址：</label>

                            <div class="col-md-6">
                                <p class="form-control-static">{$plan_and_detail_data.Inbound_shipment_plan.ship_from_address_detail}</p>
                            </div>
                        </div>
                        <div class="form-group">
                            <label class="col-md-6 control-label">所属备货需求批次号：</label>

                            <div class="col-md-6">
                                <select style="width:90%;" class="chosen-select form-control" tabindex="-1" id='batch_code' name="batch_code"
                                        data-name-group="common">
                                    <option value="" class="empty-opt">-- 请选择 --</option>
                                    <volist name="batch_code" id="bc">
                                        <option value="{$bc.id}" data-value="EB">
                                            {$bc.batch_code}
                                        </option>
                                    </volist>
                                </select>
                            </div>
                        </div>

                        <div class="form-group">
                            <label class="col-md-6 control-label">承运商服务：</label>

                            <div class="col-md-6">
                                <select style="width:90%;" class="form-control" tabindex="-1" id="carrier_service_id" name="carrier_service_id"
                                        data-name-group="common">
                                    <option value="" class="empty-opt">-- 请选择 --</option>
                                    <volist name="transport_way" id="item" >
                                        <option value="{$key}" data-value="EB">
                                            {$item}
                                        </option>
                                    </volist>
                                </select>
                            </div>
                        </div>
                        <div class="form-group">
                            <label class="col-md-6 control-label">目的地址：</label>
                            <div class="col-md-6">
                                <select style="width:85%;" class="" tabindex="-1" name="ship_to_address_id"
                                        data-name-group="common">
                                    <option value="" class="empty-opt">-- 请选择 --</option>
                                    <?php $ship_to_address_id=array_filter($ship_to_address_id);if(empty($ship_to_address_id)){?>
                                    <?php }else{foreach($ship_to_address_id as $key=>$addr){?>
                                        <option value="<?php echo $key;?>" ><?php echo $addr;?></option>
                                    <?php }}?>
                                </select>
                                <span class="text-info" style="clear:both;float:right;" data-toggle="modal" data-target=".modal-address">
                                    <font color="green" size="+2"><b>+</b></font>
                                </span>
                            </div>
                        </div>
                        <div class="form-group">
                            <label class="col-md-6 control-label">最佳到货时间：</label>
                            <div class="col-md-6">
                                <p class="form-control-static" name="claim_arrive_time"></p>
                            </div>

                        </div>
                    </div>
                    </form>

                    <div class="col-md-8">
                        <p class="text-primary"><span id="span"></span></p>

                        <div style="height: 258px; overflow-y: scroll;">
                            <table class="table table-striped  table-hover" cellspacing="0"
                                   width="100%">
                                <thead>
                                <tr>
                                    <th style="display: none">id</th>
                                    <th>SKU</th>
                                    <th>数量</th>
                                    <th>承运商服务</th>
                                    <th>公司主体</th>
                                    <th>是否出口退税</th>
                                    <th>到货时间</th>
                                </tr>
                                </thead>
                                <tbody id="mytab">

                                </tbody>
                            </table>
                        </div>
                    </div>

                <div class="clearfix"></div>
                <hr>
                <table class="table table-striped  table-hover b10" cellspacing="0" width="100%">
                    <thead>
                    <tr>
                        <th>SKU</th>
                        <th>SKU中文名称</th>
                        <th>SellerSKU</th>
                        <th>FnSKU</th>
                        <th>数量</th>
                        <th>最佳到货时间</th>
                    </tr>
                    </thead>
                    <tbody>
                    <volist name="plan_and_detail_data.Inbound_shipment_plan_detail" id="det">
                        <tr>
                            <td class="sku">{$det.sku}</td>
                            <td>{$det.sku_cname}</td>
                            <td>{$det.seller_sku}</td>
                            <td>{$det.fnsku}</td>
                            <td>{$det.quantity}</td>
                            <td class="best_arrive_time"></td>
                        </tr>
                    </volist>
                    </tbody>
                </table>
                <div class="text-center">
                    <button class="btn btn-primary" type="button" id="confirm_s">确定</button>
                </div>
            </div>
        </div>
    </div>
</div>

<!--增加地址-->
<div class="modal fade modal-address">
    <div class="modal-dialog modal-sm">

        <div class="modal-content">
            <div class="modal-header">
                <button type="button" class="close" data-dismiss="modal"><span aria-hidden="true">×</span><span
                        class="sr-only">关闭</span></button>
                <h4 class="modal-title">增加地址</h4>
            </div>
            <div class="modal-body">
                <div class="form-group">
                    <label>目的地地址：</label>
                    <textarea  id='add_address' placeholder="请输入目的地地址" class="form-control" style="width:90%;height:100px;"></textarea>
                </div>
            </div>
        </div>
        <div class="modal-footer">
            <button type="button" class="btn btn-primary" data-dismiss="modal" onclick="add_address()">确定</button>
            <button type="button" class="btn" data-dismiss="modal">取消</button>
        </div>
    </div>
</div>

<script type="text/javascript" src="__PUBLIC__/javascripts/admin/layer/layer.js"></script>
<script>
    $(function () {
        $("select[name='accountid']").val("<?php echo $_GET['accountid'];?>");
        $("input[name='shipmentid']").val("<?php  echo $_GET['shipmentid'];?>");
        $('select.chosen-select').chosen({
            no_results_text: '没有找到',    // 当检索时没有找到匹配项时显示的提示文本
            disable_search_threshold: 2, // 10 个以下的选择项则不显示检索框
            search_contains: true         // 从任意位置开始检索
        });
    });


    /**
     * 根据批次号写入数据
     */
    $('#batch_code').change(function(){
        var batch_code = $("#batch_code").find("option:selected").text();
        var accountid = $("select[name='accountid']").val();
        accountid =  $.trim(accountid);
        batch_code =  $.trim(batch_code);
        var str = '需求批次号:';
        $('#span').text(str+batch_code);
        var sku='';
        $(".sku").each(function(){
            if($(this).text())
            {
                sku += $(this).text()+',';
            }
        });
        $.ajax({
            url: "{:U('Inboundshipmentplan/get_prepareneedsdetail')}",
            type: 'post',
            async: true,
            data: {'batch_code':batch_code,'sku':sku,'accountid':accountid},
            dataType: 'json',
            success: function (data) {
                if(data.status==0){
                    layer.msg(data.info);
                    return;
                }
                if(data.flag){
                    //回写物流运输方式
//                    $("#carrier_service_id option").each(function(){
//                        if($.trim($(this).val()) == $.trim(data.flag)){
//                            $(this).attr('selected','selected');
//                            $(this).attr('readonly',true);
//                        }
//                    });
                    //回写最佳到货时间
                    $("p[name='claim_arrive_time']").text(data.min_claim_arrive_time);
                    $(".best_arrive_time").text(data.min_claim_arrive_time);
                    //回写sku
                    var html = '';
                    $.each(data.sku, function (n,data) {
                        html +=  '<tr>' +
                        '<td class="need_id" style="display: none" >' + data.id + '</td>' +
                        '<td>' + data.sku + '</td>' +
                        '<td>' + data.needs_quantity + '</td>' +
                        '<td>' + data.carrier_service_id + '</td>' +
                        '<td class="enterprise_dominant">'  + data.enterprise_dominant + '</td>'+
                        '<td class="export_tax_rebate_td">' + data.export_tax_rebate + '</td>'+
                        '<td>' + data.claim_arrive_time + '</td>'+
                        '</tr>';
                    });
                    $("#mytab").html(html);
                }
            }
        })
    });
    /**
     * 根据承运商写入数据
     */
    $('#carrier_service_id').change(function(){
        var batch_code = $("#batch_code").find("option:selected").text();
        var carrier_service_id = $("#carrier_service_id").find("option:selected").val();
        var accountid = $("select[name='accountid']").val();
        accountid =  $.trim(accountid);
        batch_code =  $.trim(batch_code);
        carrier_service_id =  $.trim(carrier_service_id);
        var sku='';
        $(".sku").each(function(){
            if($(this).text())
            {
                sku += $(this).text()+',';
            }
        });
        $.ajax({
            url: "{:U('Inboundshipmentplan/get_prepareneedsdetail')}",
            type: 'post',
            async: true,
            data: {'batch_code':batch_code,'sku':sku,'accountid':accountid,'carrier_service_id':carrier_service_id},
            dataType: 'json',
            success: function (data) {
                if(data.status==0){
                    layer.msg(data.info);
                    return;
                }
                if(data.flag){
                    //回写物流运输方式
//                    $("#carrier_service_id option").each(function(){
//                        if($.trim($(this).val()) == $.trim(data.flag)){
//                            $(this).attr('selected','selected');
//                            $(this).attr('readonly',true);
//                        }
//                    });
                    //回写最佳到货时间
                    $("p[name='claim_arrive_time']").text(data.min_claim_arrive_time);
                    $(".best_arrive_time").text(data.min_claim_arrive_time);
                    //回写sku
                    var html = '';
                    $.each(data.sku, function (n,data) {
                        html +=  '<tr>' +
                        '<td class="need_id" style="display: none">' + data.id + '</td>' +
                        '<td>' + data.sku + '</td>' +
                        '<td>' + data.needs_quantity + '</td>' +
                        '<td>' + data.carrier_service_id + '</td>' +
                        '<td class="enterprise_dominant">'  + data.enterprise_dominant + '</td>'+
                        '<td class="export_tax_rebate_td">' + data.export_tax_rebate + '</td>'+
                        '<td>' + data.claim_arrive_time + '</td>'+
                        '</tr>';
                    });
                    $("#mytab").html(html);
                }
            }
        })
    });

    //确认
    $("#confirm_s").click(function(){
        var batch_code = $("#batch_code").find("option:selected").text();
        batch_code =  $.trim(batch_code);
        //accountid
        var accountid = $("select[name='accountid']").val();
        accountid =  $.trim(accountid);
        var ship_to_address_id = $('select[name="ship_to_address_id"]').val();
        var carrier_service_id = $('select[name="carrier_service_id"]').val();
        var claim_arrive_time  = $("p[name='claim_arrive_time']").text();
        if(batch_code ==''){
            layer.msg('请选择批次号');return;
        }else if(ship_to_address_id ==''){
            layer.msg('请选择目的地址');return;
        }else if(carrier_service_id==''){
            layer.msg('请选择物流承运商');return;
        }
        var export_tax_rebate  = "";
        var enterprise_dominant  = "";
        var need_id  = "";
        var idNum = 0;
        $("#mytab .export_tax_rebate_td").each(function(){
            export_tax_rebate+=$(this).text()+',';
        });
        $("#mytab .enterprise_dominant").each(function(){
            enterprise_dominant+=$(this).text()+',';
        });
        $("#mytab .need_id").each(function(){
            need_id+=$(this).text()+',';
            idNum += 1;
        });
        var skuNum = 0;
        $(".sku").each(function(){
            if($(this).text())
            {
                skuNum += 1;
            }
        });
        if(idNum!=skuNum){
            layer.msg('备货需求数据与计划单数据不一致,请核对数据');
            return;
        }
        $.ajax({
            url: "{:U('Inboundshipmentplan/grab_confirm')}",
            type: 'post',
            async: true,
            data: {
                'batch_code':batch_code,
                'ship_to_address_id':ship_to_address_id,
                'carrier_service_id':carrier_service_id,
                'claim_arrive_time':claim_arrive_time,
                'export_tax_rebate':export_tax_rebate,
                'need_id':need_id,
                'enterprise_dominant':enterprise_dominant,
                'accountid':accountid
            },
            dataType: 'json',
            success: function (data) {
                layer.closeAll('loading');
                if(data.status==0){
                    layer.msg(data.info);
                    return;
                }
                layer.msg(data.msg);
                window.location.href= '__URL__/index?shipmentid=' + $("input[name='shipmentid']").val();
            }
        })
    });

    /*
    *新增地址
     */
    function add_address(){
        if($("select[name='accountid']").val()==''){
            layer.msg('请选择帐号');
        }
        $.ajax({
            url: "{:U('Inboundshipmentplan/add_address')}",
            type: 'post',
            async: true,
            data: {
                'accountid':$("select[name='accountid']").val(),
                'address':$("#add_address").val()
            },
            dataType: 'json',
            success: function (data) {
                if(data.status==0){
                    layer.msg(data.info);
                    return;
                }
                if(data.status ==4){
                    layer.msg('数据有误,请联系IT修正');return;
                }else if(data.status==5){
                    var html='';
                    html +=  '<option value="'+data.ship_to_address_id+'" selected="selected">' +
                    data.address
                    +'</option>';
                    $("select[name='ship_to_address_id']").html(html);
                    layer.msg('新增成功');
                }

            }
        })

    }
</script>